package com.smssalama.gui.setup;

import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemStateListener;
import javax.microedition.lcdui.StringItem;

import com.smssalama.AlertManager;
import com.smssalama.SMSSalama;
import com.smssalama.SMSSalamaSession;
import com.smssalama.Utils;
import com.smssalama.i18n.CommandLabels;
import com.smssalama.i18n.Labels;
import com.smssalama.i18n.LocalizableFactory;
import com.smssalama.i18n.ScreenTitles;

public class Start extends Form implements CommandListener{
	
	public class LocaleEntry extends StringItem{
		String localeCode;
		String name;

		public LocaleEntry(String localeCode, String name){
			super("","");
			this.setText(name);
			this.localeCode = localeCode;
			this.name = name;
		}
	}
	
	private String[][] locales = null;
	private Command exitCmd = new Command("Exit", Command.EXIT, 1);
	private Command nextCmd = new Command("Next", Command.OK, 2);
	private ChoiceGroup choiceGroup = new ChoiceGroup("Language", Choice.EXCLUSIVE);
	
	public Start() {
		super("Select Language");
		locales = Utils.getSupportedLocales();
		for(int i=0; i<locales.length; i++){
			String code = locales[i][0];
			String name = locales[i][1];
			choiceGroup.append(name, null);
		}
		this.setItemStateListener(new ItemStateListener(){
			public void itemStateChanged(Item item) {
				if(item==choiceGroup && choiceGroup.getSelectedIndex()!=-1){
					String locale = locales[choiceGroup.getSelectedIndex()][0];
					SMSSalamaSession.getSession().setLocale(locale);

					refresh();					
				}
			}});
		this.append(choiceGroup);
		this.addCommand(exitCmd);
		this.addCommand(nextCmd);
		this.setCommandListener(this);
	}
	
	public void refresh(){
		ScreenTitles SCREEN_TITLES = (ScreenTitles)LocalizableFactory.createLocalizedObject(ScreenTitles.class);
		CommandLabels cmdLabels = (CommandLabels)LocalizableFactory.createLocalizedObject(CommandLabels.class);
		Labels labels = (Labels)LocalizableFactory.createLocalizedObject(Labels.class);
		
		this.setTitle(SCREEN_TITLES.setupLanguage());
		choiceGroup.setLabel(labels.language());
		if(exitCmd!=null) this.removeCommand(exitCmd);
		exitCmd = new Command(cmdLabels.exit(), Command.EXIT, 1);
		this.addCommand(exitCmd);
		if(nextCmd!=null) this.removeCommand(nextCmd);
		nextCmd = new Command(cmdLabels.next(), Command.OK, 2);
		this.addCommand(nextCmd);
	}

	public void commandAction(Command command, Displayable arg1) {
		if(command.getCommandType()==Command.EXIT){
			SMSSalama.getInstance().quitApp();
		}else if(command.getCommandType()==Command.OK){
			if(choiceGroup.getSelectedIndex()==-1){
				AlertManager.showError("Please selected language");
			}else{
				String locale = locales[choiceGroup.getSelectedIndex()][0];
				SMSSalamaSession.getSession().setLocale(locale);

				final Setup setup = new Setup();
				AlertManager.showDisplayable(setup);
			}
		}
	}
}
